package com.tj.jinzao.presenter;


import android.content.res.AssetManager;

import com.google.gson.Gson;
import com.tj.jinzao.api.SubscriberCallBack;
import com.tj.jinzao.base.BasePresenter;
import com.tj.jinzao.base.IBaseView;
import com.tj.jinzao.manager.UserManager;
import com.tj.jinzao.model.Address;
import com.tj.jinzao.model.ProvinceCity;
import com.tj.jinzao.model.ResultResponse;
import com.tj.jinzao.utils.ToastUtils;

import org.json.JSONArray;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * 聊天处理
 * Created by wjy 2018/12/10.
 */
public class ChatPresenter extends BasePresenter<IBaseView<List<Address>>> {

    public ChatPresenter(IBaseView<List<Address>> view) {
        super(view);
    }

    public void getAddressList() {
        long uId = UserManager.getInstance().getUser().getId();
        addSubscription(mApiService.getAddressList(uId)
                , new SubscriberCallBack<List<Address>>(mView.getContext()) {
                    @Override
                    protected void onSuccess(List<Address> list) {
                        if (mView != null) mView.onRequestSuccess(list);
                    }

                    @Override
                    protected void onFailure(ResultResponse response) {
                        if (mView != null) mView.onRequestError(response.getMessage());
                    }

                });
    }

    public void addAddress(String name, String phone, String province, String city, String area, String detail) {
        long uId = UserManager.getInstance().getUser().getId();
        addSubscription(mApiService.addAddress(uId, name, phone, province, city, area, detail)
                , new SubscriberCallBack<Address>(mView.getContext()) {
                    @Override
                    protected void onSuccess(Address address) {
                        List<Address> list = new ArrayList();
                        list.add(address);
                        if (mView != null) mView.onRequestSuccess(list);
                    }

                    @Override
                    protected void onFailure(ResultResponse response) {
                        if (mView != null) mView.onRequestError(response.getMessage());
                    }

                    @Override
                    protected boolean isShowProgress() {
                        return true;
                    }
                });
    }

    public void updateAddress(int id, String name, String phone, String province, String city, String area, String detail) {
        addSubscription(mApiService.updateAddress(id, name, phone, province, city, area, detail)
                , new SubscriberCallBack<Address>(mView.getContext()) {
                    @Override
                    protected void onSuccess(Address address) {
                        List<Address> list = new ArrayList();
                        list.add(address);
                        if (mView != null) mView.onRequestSuccess(list);
                    }

                    @Override
                    protected void onFailure(ResultResponse response) {
                        if (mView != null) mView.onRequestError(response.getMessage());
                    }

                    @Override
                    protected boolean isShowProgress() {
                        return true;
                    }
                });
    }

    public void setDefaultAddress(int id) {
        addSubscription(mApiService.setDefaultAddress(id)
                , new SubscriberCallBack<String>(mView.getContext()) {
                    @Override
                    protected void onSuccess(String success) {
                        Address address1 = new Address();
                        address1.setId(-1);
                        List<Address> list = new ArrayList();
                        list.add(address1);
                        if (mView != null) mView.onRequestSuccess(list);
                    }

                    @Override
                    protected void onFailure(ResultResponse response) {
                        if (mView != null) mView.onRequestError(response.getMessage());
                    }

                    @Override
                    protected boolean isShowProgress() {
                        return true;
                    }
                });
    }

    public void deleteAddress(int id) {
        addSubscription(mApiService.deleteAddress(id)
                , new SubscriberCallBack<Address>(mView.getContext()) {
                    @Override
                    protected void onSuccess(Address address) {
                        Address address1 = new Address();
                        address1.setId(-1);
                        List<Address> list = new ArrayList();
                        list.add(address1);
                        if (mView != null) mView.onRequestSuccess(list);
                    }

                    @Override
                    protected void onFailure(ResultResponse response) {
                        if (mView != null) mView.onRequestError(response.getMessage());
                    }

                    @Override
                    protected boolean isShowProgress() {
                        return true;
                    }
                });
    }

    public void exchange(int productId, int addressId) {
        long uId = UserManager.getInstance().getUser().getId();
        addSubscription(mApiService.exchange(uId, productId, addressId)
                , new SubscriberCallBack<String>(mView.getContext()) {
                    @Override
                    protected void onSuccess(String address) {
                        if (mView != null) {
                            ToastUtils.showToast(mView.getContext(), "兑换成功");
                            mView.getContext().finish();
                        }
                    }

                    @Override
                    protected void onFailure(ResultResponse response) {
                        if (mView != null) mView.onRequestError(response.getMessage());
                    }

                    @Override
                    protected boolean isShowProgress() {
                        return true;
                    }
                });
    }

    private String getJson() {

        StringBuilder stringBuilder = new StringBuilder();
        try {
            AssetManager assetManager = mView.getContext().getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open("province.json")));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    public ArrayList<ProvinceCity> getCityList() {
        String json_data = getJson();//获取assets目录下的json文件数据

        ArrayList<ProvinceCity> cities = new ArrayList();
        try {
            JSONArray data = new JSONArray(json_data);
            Gson gson = new Gson();
            for (int i = 0; i < data.length(); i++) {
                ProvinceCity entity = gson.fromJson(data.optJSONObject(i).toString(), ProvinceCity.class);
                cities.add(entity);
            }
            return cities;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}